package com.wed.db;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ShareCmapServlet
 */
@WebServlet("/ShareCmapServlet")
public class ShareCmapServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShareCmapServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	
	}
	
	public ArrayList<String> listerror  = new ArrayList<String>();
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String user=request.getParameter("username");
		 
		 String dateShare=request.getParameter("DateShare");
		 int cmapversion=Integer.parseInt(request.getParameter("CmapVersion"));
		
		String[] checkNameShare = (request.getParameterValues("userreceive"));
		List list = Arrays.asList(checkNameShare);
		request.setAttribute("check", list);
		List<String> listuserReceive = list;
		for (String us : listuserReceive) 
		{
			ListShareStudent ls=new ListShareStudent(user, us,cmapversion, dateShare);
			ConnectDB db=new ConnectDB();
			try
			{
			
				
			if(db.insertUserShare(ls))
			{

				RequestDispatcher dispacher=request.getRequestDispatcher("BTDaLam_SV.jsp");
		    	dispacher.forward(request, response);
			}
			else
			{response.sendRedirect("error.jsp");}

				
			
			}
			catch(Exception e)
			{
				 e.printStackTrace();
			}
		}
		
		
	}

}
